package com.pq.service.impl;

import com.pq.entity.StorageDO;
import com.pq.dao.StorageDAO;
import com.pq.enums.CommonCode;
import com.pq.exception.BizServiceException;
import com.pq.service.StorageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pq
 * @since 2021-06-14
 */
@Service
@Slf4j
public class StorageServiceImpl extends ServiceImpl<StorageDAO, StorageDO> implements StorageService {

    @Transactional(rollbackFor = BizServiceException.class)
    public boolean subtract(Long id) {
        StorageDO storageDO = this.getById(id);
        if (storageDO==null){
            throw new BizServiceException(CommonCode.FAILURE.getCode(),"无效商品");
        }
        log.info("商品库存扣减");
        storageDO.setCount(storageDO.getCount()-1);
        this.updateById(storageDO);
        return true;
    }

    @Transactional(rollbackFor = BizServiceException.class)
    public boolean subtractError(Long id) {
        StorageDO storageDO = null;
        if (storageDO==null){
            throw new BizServiceException(CommonCode.FAILURE.getCode(),"无效商品");
        }
        log.info("商品库存扣减");
        storageDO.setCount(storageDO.getCount()-1);
        this.updateById(storageDO);
        return true;
    }
}
